package descentFly.away3D4
{
	import away3d.animators.data.NullAnimation;
	import away3d.entities.Mesh;
	
	import descentFly.df_spaceObject;
	
	import flash.events.IEventDispatcher;
	import flash.geom.Matrix3D;
	import flash.geom.Vector3D;
	
	public class df_path extends df_spaceObject {
		private var _list		: Array;
		private var _len		: Array;
		public function df_path(vertex: Vector.<Number>, matrix: Matrix3D) {
			super();
			initVertexs(vertex, matrix);
		}
		
		protected function initVertexs(vertex: Vector.<Number>, matrix: Matrix3D): void {
			var v			: Vector3D;
			var i			: int = 0;
			var len			: Number;
			var _distance	: Number = 0;
			
			_list 		= new Array();
			_len 		= new Array();
			while (i + 2 < vertex.length) {
				v = new Vector3D(vertex[i], vertex[i + 1], vertex[i + 2]);
				_list.push(matrix.transformVector(v));
				i += 3;
			}
			
			for (i=0; i<_list.length; i++) {
				v = _list[(i + 1) % _list.length];
				len = v.subtract(_list[i]).length;
				_len.push(_distance);
				_distance += len;
			};
			_len.push(_distance);
		}
		
		public function getVertex(index: int): Vector3D {
			return _list[index];
		}

		public function get length(): int {
			return _list.length;
		}
		
		public function getNearestPoint(v: Vector3D): int {
			var nearest	: Number = Number.MAX_VALUE;
			var index	: int = -1;
			_list.forEach(function(p: Vector3D, i: int, arr: Array): void {
				var llen: Number = p.subtract(v).length;
				if (llen < nearest) {
					index = i;
					nearest = llen;
				}
			});
			return index;
		}
		
		public function getFullDistance(cycle: Boolean = true): Number {
			return _len[_len.length - (cycle?1:2)];
		}
		
		public function indexFromPercent(percent: Number, result: Object=null, cycle: Boolean = true): int {
			var dist		: Number	= getFullDistance(cycle) * percent;
			var index		: int 		= 0;
			var offset		: Number	= 0;
			
			for (var i: int; i<_len.length; i++) {
				if (_len[i] > dist) {
					index 		= i - 1;
					offset		= dist - _len[index];
					break;
				}
			};
			if (result != null) {
				result.index	= index;
				result.offset 	= offset;
				result.distance	= dist;
			}
			return index;
		}
		
		public function percentFromIndex(index: int, cycle: Boolean = true): Number {
			return _len[index] / getFullDistance(cycle);
		}
		
		//0>=percent<=1
		public function getDataFromPercent(percent: Number, cycle: Boolean = true): Object {
			var distInfo	: Object 	= {};
			var delta		: Vector3D;
			var v_dir		: Vector3D;
			var start		: Vector3D;
			var end			: Vector3D;
			
			indexFromPercent(percent, distInfo, cycle);
			
			start 	= _list[distInfo.index];
			end		= _list[(distInfo.index + 1) % _list.length];
			delta = end.subtract(start);
			delta.normalize();
			v_dir 	= delta.clone();
			delta.scaleBy(distInfo.offset);
			
			return {
				position	: start.add(delta),
				dir			: v_dir
			}
		}
	}
}